import { _decorator, CCInteger, Component, instantiate, Label, Node, Prefab, randomRangeInt, Vec3 } from 'cc';
import { BLOCK_SIZE, PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

//地图中方块的类型
enum BlockType {
    BT_NONE,
    BT_STONE
}

//游戏状态
enum GameState {
    GS_INIT,
    GS_PLAYING,
    GS_END
}

@ccclass('GameManager')
export class GameManager extends Component {

    @property(Prefab)
    boxPrefab: Prefab | null = null //方块预制体
    @property({ type: CCInteger })
    roadLenght: number = 5  //地图总长
    private _road: BlockType[] = []  //地图，以数组的形式保存

    @property({type:Node})
    startMenu:Node|null = null  //开始菜单
    @property({type:PlayerController})
    playerCtrl:PlayerController | null = null  //玩家控制脚本
    @property(Label)
    stepsLabel:Label|null = null  //显示分数的节点

    start() {
        this.init()  //初始化并且监听“跳跃结束”事件
        this.playerCtrl.node.on('JumpEnd', this.onPlayerJumpEnd, this)
    }

    //初始化，生成地图打开开始菜单初始化角色
    init() {
        if (this.startMenu) {
            this.startMenu.active = true
        }
        this.generateRoad()
        if (this.playerCtrl) {
            this.playerCtrl.setInputActive(false)
            this.playerCtrl.node.setPosition(Vec3.ZERO)
            this.playerCtrl.reset()
        }
    }

    //生成地图，清空已有地图，循化重新生成
    generateRoad() {
        this.node.removeAllChildren()
        this._road = []
        this._road.push(BlockType.BT_STONE)

        for (let i = 1; i < this.roadLenght; i++) {
            if (this._road[i - 1] === BlockType.BT_NONE) {
                this._road.push(BlockType.BT_STONE)
            } else {
                this._road.push(randomRangeInt(0, 2))
            }
        }

        for (let j = 0; j < this._road.length; j++) {
            let block: Node | null = this.spawnBlockByType(this._road[j])
            if (block) {
                this.node.addChild(block)
                block.setPosition(j * BLOCK_SIZE, 0, 0)
            }
        }

    }

    //通过类型生成单个方块，被循环生成地图的方法调用
    spawnBlockByType(type: BlockType) {
        if (!this.boxPrefab) {
            return null
        }
        let block: Node | null
        switch (type) {
            case BlockType.BT_STONE:
                block = instantiate(this.boxPrefab)
                break;
        }
        return block
    }

    //设定当前状态，用来切换游戏状态，为了达到不同状态执行不同指令
    setCurState(state: GameState) {
        switch (state) {
            case GameState.GS_INIT:
                this.init()
                break;
            case GameState.GS_PLAYING:
                if (this.startMenu) {
                    this.startMenu.active = false
                    console.log(this.startMenu);
                    
                }
                if (this.stepsLabel) {
                    this.stepsLabel.string = '0'
                }

                //防止切换过快
                setTimeout(()=>{
                    if (this.playerCtrl) {
                        console.log(this.playerCtrl);
                        
                        this.playerCtrl.setInputActive(true)
                    }
                },0.1)
                break;
            case GameState.GS_END:

                break;
        }
    }

    //监听开始按钮
    onStartBtnClick(){
        this.setCurState(GameState.GS_PLAYING)
    }

    //监听“玩家落地”事件
    onPlayerJumpEnd(moveIndex:number){
        if (this.stepsLabel) {
            this.stepsLabel.string = '' + (moveIndex>this.roadLenght?this.roadLenght:moveIndex)
        }
        this.checkResult(moveIndex)
    }

    //判断游戏何时结束
    checkResult(moveIndex:number){
        if (moveIndex<this.roadLenght) {
            if (this._road[moveIndex]===BlockType.BT_NONE) {
            this.setCurState(GameState.GS_INIT)
            }
        }else{
            this.setCurState(GameState.GS_INIT)
        }
    }
}


